package com.gege;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;

/**
 * 测试类
 * 写一段代码，使用 HttpClient 或 OkHttp 访问 http://localhost:8801
 *
 * @author gechenpeng
 * @date 2021-03-29
 */
public class Test {

    public static void main(String[] args) throws Exception {
        String url = "http://localhost:8801";
        String data = request(url);
        System.out.println(data);
    }

    private static String request(String url) throws Exception {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet httpGet = new HttpGet(url);
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity, "UTF-8");
                EntityUtils.consume(entity);
                return data;
            }
        }
    }
}
